package com.yanqu.road.server.gameplayer.module.activity.platform;

import com.yanqu.road.dao.impl.activity.platform.UserPlatformAntiFraudDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.platform.UserPlatformAntiFraudData;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.activity.PlatformAntiFraudProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.platform.PlatformMailAntiActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

public class PlatformMailAntiActivityModule extends GeneralModule {

    UserPlatformAntiFraudData userData;

    public PlatformMailAntiActivityModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {

        ActivityInfo activityInfo = PlatformMailAntiActivityMgr.getActivityInfo();
        if(ActivityMgr.activityInShowTime(activityInfo)) {
            userData = new UserPlatformAntiFraudDataDaoImpl().getUserPlatformAntiFraudData(player.getUserId(), activityInfo.getActivityId());
        }

        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {

        if(userData != null){
            if(userData.isInsertOption()){
                new UserPlatformAntiFraudDataDaoImpl().add(userData);
            }else if(userData.isUpdateOption()){
                new UserPlatformAntiFraudDataDaoImpl().update(userData);
            }
        }
        return false;
    }

    @Override
    public void afterLogin() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.PlatformAntiFraudActivity.getValue())) {
            return;
        }
        syncUserData();
    }

    public synchronized void initUserData() {

        ActivityInfo activityInfo = PlatformMailAntiActivityMgr.getActivityInfo();

        // 没有活动
        if (!ActivityMgr.activityInTime(activityInfo)) {
            return;
        }

        // 已经有数据了
        if (userData != null && userData.getActivityId() == activityInfo.getActivityId()) {
            return;
        }

        UserPlatformAntiFraudData tmpUserData = new UserPlatformAntiFraudData();
        tmpUserData.setActivityId(activityInfo.getActivityId());
        tmpUserData.setUserId(getUserId());
        tmpUserData.setInsertOption();

        userData = tmpUserData;

    }

    /**
     * 同步玩家数据
     */
    public void syncUserData() {

        if (!ActivityMgr.activityInShowTime(PlatformMailAntiActivityMgr.getActivityInfo())) {
            return;
        }

        if(SystemOpenMgr.systemOpen(player, eSystemId.PlatformAntiFraudActivity.getValue())){
            if(userData == null){
                initUserData();
            }
        }

        if(userData == null){
            return;
        }

        PlatformAntiFraudProto.UserPlatformAntiFraudDataTemp.Builder builder = PlatformAntiFraudProto.UserPlatformAntiFraudDataTemp.newBuilder();
        builder.addAllGetReward(userData.getGetReward());
        PlatformAntiFraudProto.SyncPlatformAntiFraudDataMsg.Builder syncMsg = PlatformAntiFraudProto.SyncPlatformAntiFraudDataMsg.newBuilder();
        syncMsg.setUserData(builder);
        player.sendPacket(Protocol.U_PLATFORM_ANTI_FRAUD_DATA_SYNC, syncMsg);
    }

    public int getReward(int index) {
        if(userData == null){
            return GameErrorCode.E_ACTIVITY_OVER_TIME;
        }

        List<String> rewardList = StringUtils.stringToStringList(PlatformMailAntiActivityMgr.getActivityConfig(eActivityConfigType.ANTI_FRAUD_DIALOGUE_AWARD.getName()).getValue(), "\\|");

        PlatformAntiFraudProto.GetPlatformAntiFraudRespMsg.Builder builder = PlatformAntiFraudProto.GetPlatformAntiFraudRespMsg.newBuilder();
        if(rewardList.size() > index && index >= 0){
            if(userData.getGetReward().contains(index)){
                return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
            }
            userData.addReward(index);
            player.getModule(CurrencyModule.class).addCurrency(rewardList.get(index), eLogMoneyType.PlatformActivity, eLogMoneyType.PlatformActivityAntiReward);

            builder.setRet(0);

            PlatformAntiFraudProto.UserPlatformAntiFraudDataTemp.Builder userDataTemp = PlatformAntiFraudProto.UserPlatformAntiFraudDataTemp.newBuilder().addAllGetReward(userData.getGetReward());
            builder.setUserData(userDataTemp);
            builder.setReward(rewardList.get(index));
            player.sendPacket(Protocol.U_PLATFORM_ANTI_FRAUD_GET_REWARD, builder);

        }else {
            PlatformMailAntiActivityMgr.getLogger().error("{} get Anti Fraud Reward error. {}", player.getUserId(), index);
        }
        return 0;
    }
}
